from django.template.context_processors import request
from rest_framework import serializers
from django.contrib.auth import get_user_model
from django.core.validators import FileExtensionValidator

OAUser = get_user_model()


class AddStaffSerializer(serializers.Serializer):
    realname = serializers.CharField(max_length=20, error_messages={"required": "请输入用户名！"})
    email = serializers.EmailField(error_messages={"required": "请输入邮箱！", 'invalid': '请输入正确格式的邮箱！'})
    password = serializers.CharField(max_length=20, error_messages={"required": '请输入密码！'})

    def validate(self, attrs):
        request = self.context.get('request')
        email = attrs.get('email')
        # 1.验证邮箱是否存在
        if OAUser.objects.filter(email=email).exists():
            raise serializers.ValidationError({'email': '该邮箱已被注册'})
        # 2.验证当前用户是否是leader
        if request.user.department.leader.uid != request.user.uid:
            raise serializers.ValidationError({'detail': '您无权添加新员工'})
        return attrs


class ActiveStaffSerializer(serializers.Serializer):
    email = serializers.EmailField(error_messages={"required": "请输入邮箱！", 'invalid': '请输入正确格式的邮箱！'})
    password = serializers.CharField(max_length=20, error_messages={"required": '请输入密码！'})

    def validate(self, attrs):
        email = attrs.get('email')
        password = attrs.get('password')

        user = OAUser.objects.filter(email=email).first()
        if not user or not user.check_password(password):
            raise serializers.ValidationError('邮箱或密码错误')
        attrs['user'] = user
        return attrs


class StaffUploadSerializer(serializers.Serializer):
    file = serializers.FileField(validators=[FileExtensionValidator(['xlsx', 'xls'])],
                                 error_messages={'required': '请上传文件', 'valid_file':'请上传xlsx或xls格式的文件'})
